import pygame
import random
import time

# 初始化游戏
pygame.init()

# 设置窗口大小
window_width = 800
window_height = 600

# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

# 初始化窗口
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("贪吃蛇游戏")

# 轻松控制游戏的速度
clock = pygame.time.Clock()

# 蛇的初始位置和大小
snake_block = 20
snake_speed = 5

# 初始化字体
font = pygame.font.SysFont(None, 50)

def our_snake(snake_block, x,y):
    pygame.draw.rect(window, GREEN, [x, y, snake_block, snake_block])


def message(msg, color):
    mesg = font.render(msg, True, color)
    window.blit(mesg, [window_width / 6, window_height / 3])

# 游戏循环
game_over = False
game_close = False

x1 = window_width / 2
y1 = window_height / 2

x1_change = 0
y1_change = 0

snake_List = []
Length_of_snake = 1

foodx = round(random.randrange(0, window_width - snake_block) / snake_block) * snake_block
foody = round(random.randrange(0, window_height - snake_block) / snake_block) * snake_block

while not game_over:
    while game_close:
        window.fill(BLACK)
        message("你输了！按Q退出，按C重新开始", RED)
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    game_over = True
                    game_close = False
                if event.key == pygame.K_c:
                    game_over = False
                    game_close = False
                    x1 = window_width / 2
                    y1 = window_height / 2
                    x1_change = 0
                    y1_change = 0
                    snake_List = []
                    Length_of_snake = 1
                    foodx = round(random.randrange(0, window_width - snake_block) / snake_block) * snake_block
                    foody = round(random.randrange(0, window_height - snake_block) / snake_block) * snake_block

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT and x1_change != snake_block:
                x1_change = -snake_block
                y1_change = 0
            elif event.key == pygame.K_RIGHT and x1_change != -snake_block:
                x1_change = snake_block
                y1_change = 0
            elif event.key == pygame.K_UP and y1_change != snake_block:
                x1_change = 0
                y1_change = -snake_block
            elif event.key == pygame.K_DOWN and y1_change != -snake_block:
                x1_change = 0
                y1_change = snake_block

    if game_over:
        break

    x1 += x1_change
    y1 += y1_change

    # 检查是否与墙壁或自身碰撞
    if x1 >= window_width or x1 < 0 or y1 >= window_height or y1 < 0:
        game_close = True
    for x in snake_List[:-1]:
        if x == [x1, y1]:
            game_close = True

    window.fill(BLACK)
    pygame.draw.rect(window, RED, [foodx, foody, snake_block, snake_block])

    snake_Head = []
    snake_Head.append(x1)
    snake_Head.append(y1)
    snake_List.append(snake_Head)
    if len(snake_List) > Length_of_snake:
        del snake_List[0]

    print(snake_List)
    for (x,y) in snake_List:
        our_snake(snake_block, x,y)

    # 检查是否吃到食物
    if x1 == foodx and y1 == foody:
        Length_of_snake += 1
        foodx = round(random.randrange(0, window_width - snake_block) / snake_block) * snake_block
        foody = round(random.randrange(0, window_height - snake_block) / snake_block) * snake_block

    # 更新得分
    score = Length_of_snake - 1
    score_font = pygame.font.SysFont(None, 50)
    score_message = score_font.render(f"得分: {score}", True, WHITE)
    window.blit(score_message, (10, 10))

    pygame.display.update()

    # 控制游戏速度
    clock.tick(snake_speed)

pygame.quit()
